/**
 * 
 */
package com.cnhaimei.vod.logic;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.cnhaimei.base.TransparentActivity;
import com.cnhaimei.com.PageManager;
import com.cnhaimei.com.PageWrapper;
import com.cnhaimei.com.PageManager.PageInfoListener;
import com.cnhaimei.vod.R;
import com.cnhaimei.vod.bean.Song;
import com.cnhaimei.vod.logic.EventsManager.EventsListener;
import com.cnhaimei.vod.net.XVod;

/**
 * 已唱歌曲列表
 * 
 * @author jin001.wu@gmail.com <br>
 * @date 2011-7-8
 * 
 */
public class SingedSongListActivity extends TransparentActivity implements EventsListener, PageInfoListener  {

	private ListView listView;
	private TextView pageInoView;
	private TextView curSongView;

	private LayoutInflater mInflater;

	private AdapterEx mListAdapter;

	private PageManager pageManager;

	private ArrayList<Song> lst;

	private boolean isNeting = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.v8_singed_song_list);

		setClickable(R.id.pre_page, R.id.next_page, R.id.goback);

		mInflater = LayoutInflater.from(this);

		lst = new ArrayList<Song>(42);
		for (int i = 1; i < 4; i++) {
			for (int j = 1; j < 5; j++) {
				lst.add(Song.createSong("寒江雪" + (i * 100 + j), "郑源"
						+ (i * 100 + j), "高清"));
			}
		}
		
		initView();
		
		pageManager = new PageManager();
		pageManager.setPageButtonView(findViewById(R.id.pre_page),
				findViewById(R.id.next_page), null, null);
		pageManager.setPageInfo(1, 4, false);
		pageManager.setListener(this);
		XVod.G().registerListener(EventsManager.Event.SELCT_SONGS, this);
//		XVod.G().requestSelSong(0, 0, 5);
	
		refreshPage();
	}

	private void initView() {
		pageInoView = (TextView) findViewById(R.id.page_info);
		curSongView = (TextView) findViewById(R.id.title_tip);
		curSongView.setText(getString(R.string.has_songs, lst.size()));
		listView = (ListView) findViewById(R.id.list);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.pre_page:
			break;
		case R.id.next_page:
			break;
		case R.id.goback:
			finish();
			break;
		default:
			super.onClick(v); // need parent
			break;
		}

	}

	private void refreshPage() {
		if (mListAdapter == null) {
			mListAdapter = new AdapterEx();
			listView.setAdapter(mListAdapter);
			listView.setOnItemClickListener(mListAdapter);
		}
		mListAdapter.setList(lst);

		int total = pageManager.getTotalPage();
		pageInoView.setText(getString(R.string.page_info, pageManager
				.getCurrentIndex() + 1, total));
	}

	public class AdapterEx extends BaseAdapter implements OnItemClickListener,
			OnItemSelectedListener {

		ArrayList<Song> lst = new ArrayList<Song>();

		public void setList(ArrayList<Song> lst) {
			this.lst = lst;
		}

		public int getCount() {
			return lst.size();
		}

		public Object getItem(int position) {
			return lst.get(position);
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				//				
				convertView = mInflater.inflate(R.layout.singed_song_list_item,
						null);
				holder = new ViewHolder();
				holder.initView(convertView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.setValues((Song) getItem(position), position);

			return convertView;
		}

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			if (isNeting) {
				return;
			}
			Song r = (Song) getItem(position);
			// if (r != null && "108".equals(r.getNum())) {
			// startActivity(Const.Action.RoomMatched);
			// }

		}

		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	}

	class ViewHolder {

		public TextView order;
		public View operate;
		public TextView name;
		public TextView singer;

		public ViewHolder() {
		}

		public void initView(View layout) {
			order = (TextView) layout.findViewById(R.id.order);
			name = (TextView) layout.findViewById(R.id.name);
			singer = (TextView) layout.findViewById(R.id.singer);
			operate = layout.findViewById(R.id.operate);
		}

		public void setValues(Song data, int pos) {
			if (data == null) {
				return;
			}
			if (pos < 10) {
				order.setText("0" + pos);
			} else {
				order.setText(String.valueOf(pos));
			}
			name.setText(data.getName());
			singer.setText(data.getSinger());
		}
	}

	public void onEvent(int event, Object obj, boolean isSuc) {
		// TODO Auto-generated method stub
		
	}

	public void onPageIndexChanged(int prePageIndex, int currentPageIndex,
			int totalPages) {
		// TODO Auto-generated method stub
		
	}

	public void onPageIndexChangedError(int prePageIndex, int currentPageIndex,
			int totalPages) {
		// TODO Auto-generated method stub
		
	}

}
